package JavaDesignModel;
// 实验五 适配器模式

import java.util.*;

// 目标接口
interface DataOperation {
    void sort(int[] data);
    int search(int[] data, int target);
}

// 适配者类
class QuickSort {
    public QuickSort() {}

    public void quickSort(Integer[] dataList) {
        if (dataList == null || dataList.length == 0) return;
        quickSort(dataList, 0, dataList.length - 1);
    }

    private void quickSort(Integer[] arr, int low, int high) {
        if (low >= high) return;
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }

    private int partition(Integer[] arr, int low, int high) {
        int pivot = arr[high];
        int i = low - 1;

        for (int j = low; j < high; j++) {
            if (arr[j] <= pivot) {
                i++;
                swap(arr, i, j);
            }
        }
        swap(arr, i + 1, high);
        return i + 1;
    }

    private void swap(Integer[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

// 适配者类
class BinarySearch {
    public BinarySearch() {}

    public int binarySearch(Integer[] array, int searchData) {
        int left = 0;
        int right = array.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (array[mid] == searchData) {
                return mid;
            } else if (array[mid] < searchData) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }
}

// 适配器类
class DataOperationAdapter implements DataOperation {
    private QuickSort quickSort;
    private BinarySearch binarySearch;

    public DataOperationAdapter() {
        this.quickSort = new QuickSort();
        this.binarySearch = new BinarySearch();
    }

    @Override
    public void sort(int[] data) {
        Integer[] integerArray = new Integer[data.length];
        for (int i = 0; i < data.length; i++) {
            integerArray[i] = data[i];
        }

        quickSort.quickSort(integerArray);

        for (int i = 0; i < data.length; i++) {
            data[i] = integerArray[i];
        }
    }

    @Override
    public int search(int[] data, int target) {
        Integer[] integerArray = new Integer[data.length];
        for (int i = 0; i < data.length; i++) {
            integerArray[i] = data[i];
        }

        return binarySearch.binarySearch(integerArray, target);
    }
}

// 测试类
public class AdapterPatternDemo {
    public static void main(String[] args) {

        DataOperation operation = new DataOperationAdapter();

        int[] data = {5, 2, 8, 1, 9, 3, 7, 4, 6};
        int target = 7;

        System.out.println("测试数组: " + Arrays.toString(data));
        System.out.println("查找目标: " + target+'\n');

        operation.sort(data);
        System.out.println("排序后数组: " + Arrays.toString(data));

        int result = operation.search(data, target);
        if (result != -1) {
            System.out.println("找到目标值 " + target + " 在索引位置: " + result);
        } else {
            System.out.println("未找到目标值 " + target);
        }

    }
}